﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace Freshbox2._0.UI.Controllers
{
    public class OperateLogController : BaseController<log>
    {
        //下架操作记录

        // GET: /OperateLog/ 
        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }

        public override BLL.BaseBll<log> bll
        {
            get
            {
                return new LogBll();
            }

        }
        ManagerBll managerBll = new ManagerBll();

        StoreBll storeBll = new StoreBll();

        WaresBll waresBll = new WaresBll();

        public class ListLogModel  // logId  logTime  logType  waresCode     status   RealName   userid  storeName    waresName num
        {
            public int logId;
            public string logTime;
            public string waresCode;
            public string logType;
            public string waresName;
            public string RealName;
            public int userid;
            public string storeName;
            public string logReason;
            public int? status;
            public int num;
            public string downWaresState;
        }

        /// <summary>
        /// 获取下架操作记录 列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="model"></param>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        public ActionResult GetNotice(int page, int rows, log model,string date1, string date2)
        {
            //int count;
            var listLogModel = new List<ListLogModel>();

            //1. 查询出意见表表里的所有的信息                           
            Expression<Func<log, bool>> condition1 = x => x.logType == 0;

            if (string.IsNullOrEmpty(date1) || string.IsNullOrEmpty(date2))
            {
                // date1 date2为空，代表第一次查询 或者是没有选日期的查询，默认选查询所有的数据
                condition1 = x => x.logType == 0; //
            }
            else
            {
                // 1.先筛选出这个时间段的 满足要求的信息   
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition1 = x => x.logTime >= datetime1 && x.logTime <= datetime2 && x.logType == 0;
            }
            //model, x => true, true, x => x.logId, rows, page, out count
            var listNotice = bll.Search().Where(condition1).Select(x => new
            {
                x.logId,
                x.managerCode,
                x.status,
                x.logTime,
                logType = x.logType == 1 ? "上架" : "下架",
                x.storeCode,
                x.waresCode,
                x.logReason,
                x.afterUpdate,
                x.beforeUpdate,
                waresName=x.wares.waresName,
                downWaresState = x.downWaresState == 0 ? "请耐心等待，商品在路上" : x.downWaresState == 1 ? "下架商品已经送回仓库":" ",
            }).ToList();

            foreach (var oneCode in listNotice)
            {
                int managerid = Convert.ToInt32(oneCode.managerCode);

                //根据订单详情表里的商品编号  进行查询用户表的相关信息   

                var list2 = managerBll.Search(x => x.userid == managerid).Select(x => new { x.RealName, x.userid, }).ToList();

                var storeList = storeBll.Search(x => x.storeid == oneCode.storeCode).Select(x => new { x.storeid, x.storeName, x.storeDress, x.property }).ToList();

               //var wareList = waresBll.Search(x => x.waresCode == oneCode.waresCode).Select(x => new { x.waresCode, x.waresName, }).ToList();

                //判断 驳回的操作是否被理货员处理  >=1 代表已经处理 && wareList.Count() != 0
                int isRun = bll.Search(x => x.managerCode == oneCode.managerCode && x.logType == 1 && x.waresCode == oneCode.waresCode && x.storeCode == oneCode.storeCode && x.logTime >= oneCode.logTime).Count();


                if (list2.Count != 0 && storeList.Count != 0 )
                {
                    ListLogModel logmodel = new ListLogModel();
                    logmodel.logId = oneCode.logId;
                    logmodel.logType = oneCode.logType;   //double aaaaa = Math.abs(你的变量);  求绝对值

                    logmodel.num = Math.Abs(Convert.ToInt32(oneCode.beforeUpdate) - Convert.ToInt32(oneCode.afterUpdate));
                    if (isRun >= 1)
                    {
                        logmodel.status = 2;//采销人员 已处理   
                    }
                    else
                    {
                        logmodel.status = oneCode.status;

                    }
                    logmodel.waresCode = oneCode.waresCode;
                    logmodel.waresName = oneCode.waresName;
                    logmodel.storeName = storeList.FirstOrDefault().storeName;
                    logmodel.userid = list2.FirstOrDefault().userid;
                    logmodel.logReason = oneCode.logReason;
                    logmodel.RealName = list2.FirstOrDefault().RealName;
                    logmodel.logTime = Convert.ToDateTime(oneCode.logTime).ToString("yyyy-MM-dd HH:mm:ss");
                    logmodel.downWaresState = oneCode.downWaresState;
                    listLogModel.Add(logmodel);
                }
            }
            //下面是修改 读取状态  已读取  未读取  
            var listNotice2 = bll.Search(x => x.IsRead == 0).Where(condition1).Select(x => new
            {
                x.logId,
                x.managerCode,
                x.status,
                x.logTime,
                logType = x.logType,
                x.storeCode,
                x.waresCode,
                x.logReason,
                x.afterUpdate,
                downWaresState = x.downWaresState,
                x.beforeUpdate
            }).ToList();
            foreach (var item in listNotice2)
            {
                log model1 = new log();
                model1.logId = item.logId;
                model1.managerCode = item.managerCode;
                int logtype = Convert.ToInt32(item.logType);
                model1.logType = 0;
                model1.logTime = item.logTime;

                model1.waresCode = item.waresCode;
                model1.logReason = item.logReason;

                model1.afterUpdate = item.afterUpdate;
                model1.beforeUpdate = item.beforeUpdate;
                model1.status = item.status;
                model1.downWaresState = item.downWaresState;
                model1.storeCode = item.storeCode;
                model1.IsRead = 1;//已读
                if (bll.Modify(model1) > 0)
                {

                }
            }
           var listLog = listLogModel.OrderByDescending(x=>x.logId).ToPagedList(page, rows).ToList();
           return Json(new { rows = listLog, total = listLogModel.Count() });
        }

        MailContentBll mailContentBll = new MailContentBll();

        MailBll mailBll = new MailBll();

        /// <summary>
        /// 驳回消息      
        /// </summary>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public ActionResult Reject(int logId, int userid, string RealName, string waresName, string waresCode, string logType, int num, string storeName, mailcontent contentModel)
        {
            string msg = "";
            int result = 0;
            contentModel.mcTime = DateTime.Now;
            contentModel.mcType = 1;
            DateTime? date = bll.Search(x => x.logId == logId).Select(x => new { x.logTime }).FirstOrDefault().logTime;
            contentModel.mcValue = RealName + " 理货员,您" + date + "" + logType + "  的" + storeName + "店铺的，" + "商品编号为：" + waresCode + ",商品名称为:" + waresName + "的商品，该操作被管理员驳回，请你及时进行修改，如有疑问，请联系管理员。";
            //先在 mailContent 表里面添加记录
            if (mailContentBll.Add(contentModel) > 0)
            {
                //然后再mail  表里面添加了记录
                mail mailModel = new mail();
                mailModel.fromManager = 0;
                mailModel.toManager = userid;
                mailModel.mfDate = DateTime.Now;
                mailModel.mcId = contentModel.mcId;
                mailModel.status = 1;
                mailModel.mfId = 0;

                if (mailBll.Add(mailModel) > 0)
                {
                    log logModel = new log();
                    var list = bll.Search(x => x.logId == logId).Select(x => new { x.logId, x.logTime, x.logType, x.logReason, x.waresCode, x.managerCode, x.status, x.storeCode, x.afterUpdate, x.beforeUpdate ,x.downWaresState}).ToList();
                    logModel.logId = logId;
                    logModel.logTime = list.FirstOrDefault().logTime;
                    logModel.logType = list.FirstOrDefault().logType;
                    logModel.logReason = list.FirstOrDefault().logReason;
                    logModel.waresCode = list.FirstOrDefault().waresCode;
                    logModel.managerCode = list.FirstOrDefault().managerCode;
                    logModel.status = 0;
                    logModel.storeCode = list.FirstOrDefault().storeCode;
                    logModel.afterUpdate = list.FirstOrDefault().afterUpdate;
                    logModel.beforeUpdate = list.FirstOrDefault().beforeUpdate;
                    logModel.downWaresState = list.FirstOrDefault().downWaresState;
                    ////  修改 驳回 状态   
                    if (bll.Modify(logModel) > 0)
                    {
                        msg = "驳回成功";
                        result = 1;
                    }
                }
            }
            return Json(new { state = result, msg = msg });
        }
    }
}
